package com.ynsj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynsj.mapper.SysPrivilegeMapper;
import com.ynsj.domain.SysPrivilege;
import com.ynsj.service.SysPrivilegeService;
import org.springframework.util.CollectionUtils;

@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService{

    @Autowired
    private SysPrivilegeMapper privilegeMapper;

    /**
     * 分页查询权限数据
     * @param privilegePage
     * @return
     */
    @Override
    public Page<SysPrivilege> findPrivilegePage(Page<SysPrivilege> privilegePage) {
        //对页数据按修改时间排序
        privilegePage.addOrder(OrderItem.desc("last_update_time"));
        return page(privilegePage);
    }

    /**
     * 新增权限数据
     * @param sysPrivilege
     * @return
     */
    @Override
    public boolean addPrivilege(SysPrivilege sysPrivilege) {
        //需要给我们的新增对象填充一些属性
        /*String userIdStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        sysPrivilege.setCreateBy(Long.valueOf(userIdStr));
        sysPrivilege.setCreated(new Date());
        sysPrivilege.setLastUpdateTime(new Date());*/
        return save(sysPrivilege);
    }

    /**
     * 修改权限数据
     * @param sysPrivilege
     * @return
     */
    @Override
    public boolean updatePrivilege(SysPrivilege sysPrivilege) {
        /*String userIdStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        sysPrivilege.setModifyBy(Long.valueOf(userIdStr));
        sysPrivilege.setLastUpdateTime(new Date());*/
        return updateById(sysPrivilege);
    }

    @Override
    public boolean deletePrivilege(String id) {
        return removeById(id);
    }

    /**
     * 获取该菜单下该角色的权限数据
     *
     * @param roleId
     * @param menuId
     * @return
     */
    @Override
    public List<SysPrivilege> getAllSysPrivileges(Long roleId, Long menuId) {
        //获取该菜单下所有的权限
        List<SysPrivilege> sysPrivileges = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId));
        if (CollectionUtils.isEmpty(sysPrivileges)){
            return Collections.emptyList();
        }
        //将角色包含的权限放进去
        sysPrivileges.forEach(sysPrivilege -> {
            Set<Long> currentRoleSysPrivilegeIds = privilegeMapper.getPrivilegeByRole(roleId);
            if (currentRoleSysPrivilegeIds.contains(sysPrivilege.getId())){
                sysPrivilege.setOwn(1);//当前角色拥有该权限
            }
        });
        return sysPrivileges;
    }
}
